﻿Imports System.Drawing
Imports System.Threading
Imports System.Net

Public Class ImageProxy
    Private m_pictureBox As PictureBox

    Public Sub New(ByVal pbxDisplay As PictureBox)
        m_pictureBox = pbxDisplay
    End Sub

    Public Sub DisplayImageFromURL(ByVal imageUrl As String)
        DisplayTemporaryImage()
        Dim thread As New Thread(AddressOf GetImage)
        thread.Start(imageUrl)
    End Sub

    Private Sub DisplayTemporaryImage()
        Dim bitmap As New Bitmap(800, 600)
        Dim g As Graphics = Graphics.FromImage(Bitmap)
        Dim font As New Font("Arial", 24)
        Dim b As SolidBrush = Brushes.Black
        g.DrawString("Loading CD cover, please wait...", font, b, 160, 220)

        m_pictureBox.Image = bitmap
    End Sub

    Private Sub GetImage(ByVal obj As Object)
        'Simulate some time elapsed
        Thread.Sleep(1000)

        Dim uri As String = CType(obj, String)
        Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
        request.Method = "GET"
        request.Timeout = 60000
        Dim response As HttpWebResponse = request.GetResponse

        m_pictureBox.Image = Image.FromStream(response.GetResponseStream)
    End Sub
End Class
